﻿/*标记地图模块化*/
/*驾车*/
var polyline = null;
var point1 = null;
var point2 = null;
var points = null;
var newPoints = null;

var ISS_Map_driving = {
    //驾车线路方案检索
    DrivingScheme: function (start, end, startname, endname, policy) {
        var policy_enum = 0;
        //最小时间
        if (policy == "BMAP_DRIVING_POLICY_LEAST_TIME") {
            policy_enum = 1;
        }
        //最短距离
        else if (policy == "BMAP_DRIVING_POLICY_LEAST_DISTANCE") {
            policy_enum = 2;
        }
        //避免高速
        else if (policy == "BMAP_DRIVING_POLICY_AVOID_HIGHWAYS") {
            policy_enum = 3;
        }
        this.selectMapPoint = function () {
            var driving = new BMap.DrivingRoute(ISS_MAP.nearByMap, { renderOptions: { map: ISS_MAP.nearByMap, panel: "r-result1" }, policy: policy_enum
            });
            var searchResult = true;

            jQuery("#pickresult").html('');  //先清空历史记录
            ISS_MAP.ClearArc(); //清除所有覆盖物

            driving.setSearchCompleteCallback(function (results) {
                if (driving.getStatus() == BMAP_STATUS_SUCCESS) {
                    policy = 'BMAP_DRIVING_POLICY_LEAST_TIME';
                    // 获取第一条方案
                    var plan = results.getPlan(0);
                    // 获取方案的驾车线路
                    var route = plan.getRoute(0);
                    switch (policy) {
                        case "BMAP_DRIVING_POLICY_LEAST_TIME":
                            {
                                // 获取每个关键步骤,并输出到页面
                                var s = [];
                                s.push("<div class=\"search-info\">约" + plan.getDistance(true) + "/" + plan.getDuration(true) + "</div>");
                                s.push("<div class=\"search-result\" id=\"walk_Result\">");
                                s.push("<h5><a href=\"#\"><p><i class=\"ico-start\"></i>起点:" + startname + "</p></a></h5>");
                                for (var index = 0; index < route.getNumSteps(); index++) {
                                    var step = route.getStep(index)
                                    s.push("<dl>");
                                    s.push("<dt><i>" + parseInt(index + 1) + ".</i>" + step.getDescription() + "</dt><dd></dd>");
                                    s.push("</dl>");
                                }
                                s.push("<h5><a href=\"#\"><p><i class=\"ico-end\"></i>终点:" + endname + "</p></a></h5>");
                                s.push("</div>");
                                jQuery("#pickresult").html(s.join(""));
                                break;
                            }
                    }
                }
                else {
                    jQuery("#pickresult").css("display", "block");
                    jQuery("#pickresult").html("<div class=\'search-info\'>未找到相关驾车信息</div>"); //驾车行查询提示
                    searchResult = false;
                }
                //显示结果
                setTimeout(
                    function () { ISS_MAP.PickModal(searchResult); },
                    1000);
            });
            driving.search(start, end);
        }
    },
    //根据所有点，和定义的两个点，获取中间所有点返回
    GetPoints: function (points, point1, point2) {
        var startIndex = 0;
        var findstar = false;
        var endIndex = 0;
        var findend = false;
        for (var i = 0; i < points.length; i++) {
            if ((points[i].lat != point1.lat || points[i].lng != point1.lng) && !findstar) {
                startIndex++;
            } else {
                findstar = true;
            }
            if ((points[i].lat != point2.lat || points[i].lng != point2.lng) && !findend) {
                endIndex++;
            } else {
                findend = true;
            }
        }
        //获取新的点集合
        var newPoints = new Array();
        for (var i = 0; i < endIndex - startIndex + 1; i++) {
            newPoints[i] = new BMap.Point(points[startIndex + i].lng, points[startIndex + i].lat);
        }
        return newPoints;
    },
    //添加覆盖物
    AddOverlays: function (results, _map) {

        // 自行添加起点和终点
        var start = results.getStart();
        var end = results.getEnd();
        //使用百度自带起始点坐标图片
        this.AddStart(start.point, start.title);
        this.AddEnd(end.point, end.title);
        var viewPoints = [start.point, end.point];
        // 获取方案
        var plan = results.getPlan(0);
        // 获取方案中包含的路线
        for (var i = 0; i < plan.getNumRoutes(); i++) {
            ISS_Map_driving.AddRoute(plan.getRoute(i).getPath(), _map);
            viewPoints.concat(plan.getRoute(i).getPath());
        }
        // 设置地图视野
        _map.setViewport(viewPoints, {
            margins: [40, 10, 10, 10]
        });
    },
    //添加step移动进入mouseover覆盖物
    AddRedOverlays: function (points) {
        polyline = new BMap.Polyline(points, { strokeColor: "#FF1111", strokeWeight: 6, strokeOpacity: 1 });
        map.addOverlay(polyline);
    },
    //添加step移出mouseout覆盖物
    RemoveRedOverlays: function () {
        map.removeOverlay(polyline);
    },
    //添加方案描述
    AddText: function (results) {

        var plan = results.getPlan(0);
        // 获取方案中包含的路线;
        var htmls = "";
        for (var i = 0; i < plan.getNumRoutes(); i++) {
            var route = plan.getRoute(i);
            for (var j = 0; j < route.getNumSteps(); j++) {
                var curStep = route.getStep(j);
                htmls += "<div style='line-height:20px'>" + (j + 1) + '. ' + curStep.getDescription() + '</div>';
            };
        };
        jQuery("#r-result").html(htmls);
    },
    //添加路线
    AddRoute: function (path, _map) {
        _map.addOverlay(new BMap.Polyline(path, {
            strokeColor: '#0000FF',
            enableClicking: false
        }));
    },
    //添加起点覆盖物
    AddStart: function (point, title) {

        var url = "http://map.baidu.com/image/dest_markers.png";
        var width = 42;
        var height = 34;
        var myIcon = new BMap.Icon(url, new BMap.Size(width, height), { offset: new BMap.Size(14, 32), imageOffset: new BMap.Size(0, 0) });
        map.addOverlay(new BMap.Marker(point, {
            title: title,
            //icon: new BMap.Icon("../../../Content/Themes/Default/Img/Trip/Map/start_pot.png", new BMap.Size(25, 28))
            icon: myIcon
        }));

    },
    //添加终点覆盖物
    AddEnd: function (point, title) {
        var url = "http://map.baidu.com/image/dest_markers.png";
        var width = 42;
        var height = 34;
        var myIcon = new BMap.Icon(url, new BMap.Size(width, height), { offset: new BMap.Size(14, 32), imageOffset: new BMap.Size(0, -34) });
        map.addOverlay(new BMap.Marker(point, {
            title: title,
            //icon: new BMap.Icon("../../../Content/Themes/Default/Img/Trip/Map/end_pot.png", new BMap.Size(25, 28))
            icon: myIcon
        }))
    },
    //驾车搜索结果tab切换
    SearchDrivingTab: function (id) {
        var car_scheme = "BMAP_DRIVING_POLICY_LEAST_TIME";
        switch (id) {
            case 0:
                car_scheme = "BMAP_DRIVING_POLICY_LEAST_TIME";
                break;
            case 1:
                car_scheme = "BMAP_DRIVING_POLICY_LEAST_DISTANCE";
                break;
            case 2:
                car_scheme = "BMAP_DRIVING_POLICY_AVOID_HIGHWAYS";
                break;
            default:
                car_scheme = "BMAP_DRIVING_POLICY_LEAST_TIME";
                break;
        }
        //定义起始点
        var startpoint;
        var endpoint;
        if (ISS_MAP_SEARCH_START.Lng && ISS_MAP_SEARCH_START.Lng != ''
        && ISS_MAP_SEARCH_START.Lat && ISS_MAP_SEARCH_START.Lat != '') {
            startpoint = new BMap.Point(ISS_MAP_SEARCH_START.Lng, ISS_MAP_SEARCH_START.Lat);
        } else {
            startpoint = ISS_MAP_SEARCH_START.Name;
        }
        if (ISS_MAP_SEARCH_END.Lng && ISS_MAP_SEARCH_END.Lng != ''
        && ISS_MAP_SEARCH_END.Lat && ISS_MAP_SEARCH_END.Lat != '') {
            endpoint = new BMap.Point(ISS_MAP_SEARCH_END.Lng, ISS_MAP_SEARCH_END.Lat);
        } else {
            endpoint = ISS_MAP_SEARCH_END.Name;
        }
        var dr_Scheme = new ISS_Map_driving.DrivingScheme(startpoint, endpoint, ISS_MAP_SEARCH_START.Name, ISS_MAP_SEARCH_END.Name, car_scheme);
        dr_Scheme.selectMapPoint();
    }
}